<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class OrderItemsModel extends Model {

    protected $table ="order_items";

    protected $pk = 'order_item_id';

    private $cacheKey="order:item:id:";

    private $cacheTime=5184000;


    /*
     * 批量生成订单
     * */
    public function batchAdd($list){
        return Db::name('order_items')->insertAll($list);
    }


    /*
     * 获取订单规格的数量
     * */
    public function getOrderItemsSpecNum($order_id){
        $where =[];
        $where['order_id'] = $order_id;
        $num = Db::name('order_items')->where($where)->sum("spec_num");
        return $num;
    }


    /*
     * 获取订单子项
     * */
    public function getOrderItemsList($order_id){
        if(empty($order_id)){
            return [];
        }
        $where =[];
        $where['order_id'] = $order_id;
        return $this->getList($where);
    }


    /*
     * 获取total
     * */
    public function getTotal($where=[]){
        return Db::name('order_items')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('order_items')->field("order_item_id")->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['order_item_id']);
            }
            return $return;
        }
        return [];
    }



    /*
   * 获取订单状态详情
   * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name('order_items')->find($id);
            $info['format_spec_total_price'] = round( $info['spec_total_price'] / 100, 2);
            $info['format_spec_total_pay_price'] = round( $info['spec_total_pay_price'] / 100, 2);
            $info['format_spec_total_discount_price'] = round( $info['spec_total_discount_price'] / 100, 2);
            $info['format_spec_price'] = round( $info['spec_price'] / 100, 2);
            $info['format_spec_pay_price'] = round( $info['spec_pay_price'] / 100, 2);
            $info['format_spec_discount_price'] = round( $info['spec_discount_price'] / 100, 2);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }

        if($info){
            $info["spec_ids"] = !empty($info["spec_ids"]) ? json_decode($info["spec_ids"],true) : "";
            $info["product_snapshot"] = !empty($info["product_snapshot"]) ? json_decode($info["product_snapshot"],true) : "";
            $info["spec_snapshot"] = !empty($info["spec_snapshot"]) ? json_decode($info["spec_snapshot"],true) : "";
            return $info;
        }
    }



    /*
     * 根据订单获取商品数量列表
     * */
    public function getProductSaleNumList($order_id){
        $where =[];
        $where['order_id'] = $order_id;
        return Db::name('order_items')->field("product_id,spec_id,spec_num")->where($where)->select();
    }


    //获取商品快照List
    public function getProductSnapshoList($order_ids){
        $where =[];
        $where[] = ["order_id","IN",$order_ids];
        return Db::name('order_items')->field("product_snapshot")->where($where)->select();
    }

    public function batchUpdate($datas)
    {
        return $this->saveAll($datas);
    }
}